﻿@using UnionistsManagement_DaLat.Areas.Admin.Models
<div class="panel panel-primary">
    <!-- Default panel contents -->
    <div class="panel-heading">Danh sách các chức năng</div>
    <div class="panel-body">
        <p><input type="checkbox" id="checkAll" /> Chọn tất cả</p>
        @{
            var functionCatalog = (List<FunctionCatalog>)ViewBag.FunctionCatalogs;
            var listFunc = (List<Access>)ViewBag.ListFunc;
            if (listFunc != null && listFunc.Count > 0)
            {
                var list = listFunc.Select(x => x.Function.ID);

                foreach (var funCatalog in functionCatalog)
                {
                    <div class="box-function">
                        <p><input type="checkbox" class="checkAll" />&nbsp; <span class="funCatalog-title">@funCatalog.Name</span>  </p>
                        <div class="functions-list">
                            @foreach (var func in funCatalog.Functions)
                            {
                                if (list.Contains(func.ID))
                                {
                                    <label class="checkbox-label"><input type="checkbox" checked="checked" name="functionChecked" value="@func.ID" /> @func.Name</label> @:&nbsp;
                            }
                                else
                                {
                                    <label class="checkbox-label"><input type="checkbox" name="functionChecked" value="@func.ID" /> @func.Name</label> @:&nbsp;
                            }

                            }
                        </div>
                    </div>
                }
            }
            else
            {
                foreach (var funCatalog in functionCatalog)
                {
                    <div class="box-function">
                        <p><input type="checkbox" class="checkAll" />&nbsp; <span class="funCatalog-title">@funCatalog.Name</span>  </p>
                        <div class="functions-list">
                            @foreach (var func in funCatalog.Functions)
                            {

                                <label class="checkbox-label"><input type="checkbox" name="functionChecked" value="@func.ID" /> @func.Name</label> @:&nbsp;


                            }
                        </div>
                    </div>
                }
            }

        }
    </div>

</div>


<script>
    $(document).ready(function () {
        $(".funCatalog-title").click(function () {
            $(this).parents().next(".functions-list").toggle(500);
        });
    });

    $(".checkAll").click(function () {
        var check = $(".checkAll").is(":checked");
        $(this).parents().next(".functions-list").find("input[type='checkbox']").prop("checked", check);

    });


    $("#checkAll").click(function () {
        var check = $("#checkAll").is(":checked");
        $(this).parents().find("input[type='checkbox']").prop("checked", check);

    });



</script>